package eu.dominicum.ft.ui.web.jsf.view.bean;

import eu.dominicum.ft.pd.Country;
import eu.dominicum.ft.pd.Person;
import eu.dominicum.ft.pd.ProfessionalCyclist;
import eu.dominicum.ft.pd.boundary.GeneralManagement;
import eu.dominicum.ft.pd.boundary.LocationManagement;
import eu.dominicum.ft.pd.mao.MakeProfessionalCyclistMAO;
import org.apache.myfaces.trinidad.context.RequestContext;

import javax.annotation.PostConstruct;
import javax.ejb.EJBException;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;

/**
 * Backing bean for the make-professional-cyclist-dialog view.
 *
 * @author Harry Klerks
 * @since 2013M1
 */
@ViewScoped
@ManagedBean(name = "makeProfessionalCyclistDialog")
public final class MakeProfessionalCyclistDialogBean {

    /**
     * Person select items.
     */
    private final List<SelectItem> personSelectItems = new ArrayList<>();

    /**
     * Nationality (country) select items.
     */
    private final List<SelectItem> nationalitySelectItems = new ArrayList<>();

    /**
     * GeneralManagement.
     */
    @Inject
    private GeneralManagement generalManagement;

    /**
     * LocationManagement.
     */
    @Inject
    private LocationManagement locationManagement;

    /**
     * MakeProfessionalCyclistMAO.
     */
    @Inject
    private MakeProfessionalCyclistMAO makeProfessionalCyclistMAO;

    /**
     * Default contructor.
     */
    public MakeProfessionalCyclistDialogBean() {

    }

    /**
     * Initialiser.
     */
    @PostConstruct
    public void init() {

        List<Person> persons = this.generalManagement.findAllPerson();
        for (Person person : persons) {
            this.personSelectItems.add(new SelectItem(person.getId(), person.getOrderedName()));
        }

        List<Country> countries = this.locationManagement.findAllCountries();
        for (Country country : countries) {
            this.nationalitySelectItems.add(new SelectItem(country.getId(), country.getName()));
        }
    }

    /**
     * .
     *
     * @return List<SelectItem>
     */
    public List<SelectItem> getPersonSelectItems() {
        return personSelectItems;
    }

    /**
     * .
     *
     * @return List<SelectItem>
     */
    public List<SelectItem> getNationalitySelectItems() {
        return nationalitySelectItems;
    }

    /**
     * .
     *
     * @return MakeProfessionalCyclistMAO
     */
    public MakeProfessionalCyclistMAO getMakeProfessionalCyclistMAO() {
        return makeProfessionalCyclistMAO;
    }

    /**
     * Action event handler that fires when the Maak button is clicked.
     *
     * @param actionEvent -
     */
    public void makeProfessionalCyclist(final ActionEvent actionEvent) {

        try {
            ProfessionalCyclist newProfessionalCyclist = this.generalManagement.makeProfessionalCyclist(this.makeProfessionalCyclistMAO);
            RequestContext.getCurrentInstance().returnFromDialog(newProfessionalCyclist, null);
        } catch (EJBException eex) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    eex.getMessage(), null));
        }
    }
}
